<template>
  <NuxtLayout>
    <el-breadcrumb class="my-crumb">
      <el-breadcrumb-item v-for="(item, index) in crumbData" :key="index">
        <a :href="item.href">{{ item.title }}</a>
      </el-breadcrumb-item>
    </el-breadcrumb>
    <div class="con">
      <div class="con_cen">
        <div class="con_cen_left">
          <div class="con_cen_left_top">
            <img class="con_cen_left_top_icon" src="https://static.dzyxzx.cn/jkbpc/exam_logo.png" />
            <div class="con_cen_left_top_item">
              <strong>题库选择</strong>
              <p>按分类选择题库类别</p>
            </div>
          </div>
          <div class="con_cen_left_bottom">
            <ul class="con_cen_left_bottom_ul">
              <li class="con_cen_left_bottom_ul_li" v-if="subjectData.length > 0">
                <a v-for="item in subjectData" :key="item.id" class="el-button el-button--primary el-button--small"
                  :class="all[0] == item.id ? '' : 'is-plain'" :href="`/exercises/${item.id}`" :title="item.name">{{
                    item.name }}</a>
              </li>
              <li class="con_cen_left_bottom_ul_li" v-if="subjectTwoData.length > 0">
                <a v-for="item in subjectTwoData" :key="item.id" class="el-button el-button--primary el-button--small"
                  :class="all[1] == item.id ? '' : 'is-plain'" :href="item.href" :title="item.name">{{
                    item.name }}</a>
              </li>
              <li class="con_cen_left_bottom_ul_li" v-if="subjectThreeData.length > 0">
                <a v-for="item in subjectThreeData" :key="item.id" class="el-button el-button--primary el-button--small"
                  :class="all[2] == item.id ? '' : 'is-plain'" :href="item.href" :title="item.name">{{
                    item.name }}</a>
              </li>
            </ul>
          </div>
        </div>
        <div class="con_cen_right">
          <div class="con_cen_right_top">
            <div class="con_cen_right_top_link" :href="examHref" @click="examModalFn"
              v-if="useToken().token && hrefFlag">
              <strong>模拟考试</strong>
              <span>模拟在线真实考场</span>
            </div>
            <div class="con_cen_right_top_link" @click="exercisesFn()" v-else>
              <strong>模拟考试</strong>
              <span>模拟在线真实考场</span>
            </div>
            <a class="con_cen_right_top_link" :href="selectedHref" v-if="useToken().token && hrefFlag && selectedHref">
              <strong>精选练题</strong>
              <span>按顺序练习做题</span>
            </a>
            <div class="con_cen_right_top_link" @click="exercisesFn('selected')" v-else>
              <strong>精选练题</strong>
              <span>按顺序练习做题</span>
            </div>
          </div>
          <div class="con_cen_right_bottom">
            <a class="con_cen_right_bottom_link" :href="allHref" v-if="useToken().token && hrefFlag && allHref">
              <img class="con_cen_right_bottom_link_icon" src="https://static.dzyxzx.cn/jkbpc/exam_c.png" />
              <div class="con_cen_right_bottom_link_item">
                <strong>全部习题</strong>
                <p>共{{ exercisesCountData.all }}道题</p>
              </div>
            </a>
            <div class="con_cen_right_bottom_link" @click="exercisesFn('all')" v-else>
              <img class="con_cen_right_bottom_link_icon" src="https://static.dzyxzx.cn/jkbpc/exam_c.png" />
              <div class="con_cen_right_bottom_link_item">
                <strong>全部习题</strong>
                <p>共{{ exercisesCountData.all }}道题</p>
              </div>
            </div>
            <!-- <a class="con_cen_right_bottom_link" :href="doneHref" v-if="useToken().token && hrefFlag && doneHref">
              <img class="con_cen_right_bottom_link_icon" src="https://static.dzyxzx.cn/jkbpc/exam_d.png" />
              <div class="con_cen_right_bottom_link_item">
                <strong>未做习题</strong>
                <p>共{{ exercisesCountData.notDone }}道题</p>
              </div>
            </a>
            <div class="con_cen_right_bottom_link" @click="exercisesFn('done')" v-else>
              <img class="con_cen_right_bottom_link_icon" src="https://static.dzyxzx.cn/jkbpc/exam_d.png" />
              <div class="con_cen_right_bottom_link_item">
                <strong>未做习题</strong>
                <p>共{{ exercisesCountData.notDone }}道题</p>
              </div>
            </div> -->
            <a class="con_cen_right_bottom_link" :href="correctHref" v-if="useToken().token && hrefFlag && correctHref">
              <img class="con_cen_right_bottom_link_icon" src="https://static.dzyxzx.cn/jkbpc/exam_e.png" />
              <div class="con_cen_right_bottom_link_item">
                <strong>我的错题</strong>
                <p>共{{ exercisesCountData.wrong }}道题</p>
              </div>
            </a>
            <div class="con_cen_right_bottom_link" @click="exercisesFn('correct')" v-else>
              <img class="con_cen_right_bottom_link_icon" src="https://static.dzyxzx.cn/jkbpc/exam_e.png" />
              <div class="con_cen_right_bottom_link_item">
                <strong>我的错题</strong>
                <p>共{{ exercisesCountData.wrong }}道题</p>
              </div>
            </div>
          </div>
          <div class="con_cen_right_clear">
            <span class="con_cen_right_clear_link" @click="clearExamFn">清除习题记录</span>
          </div>
        </div>
      </div>
    </div>
    <!-- 考试须知 -->
    <el-dialog v-model="examModalVisible" title="考试须知" width="500" :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false">
      <div v-html="examNotice"></div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="examModalVisible = false">取消</el-button>
          <el-button type="primary" @click="examStartFn">开始考试</el-button>
        </div>
      </template>
    </el-dialog>
  </NuxtLayout>
</template>

<script setup>
import { getQuestionListApi, getCountApi, clearExerciseRecord } from '~/server/api'
import { treeFn } from '~/utils/utils'
import { useCountStore } from "~/store/counter.js";
import { useToken } from '~/store/token';
import useStore from 'element-plus/es/components/table/src/store/index.mjs';
useCountStore().$patch({
  indexActive: 4
});
const crumbData = ref([
  { title: '首页', href: '/' },
  { title: '题库中心', href: 'javascript:;' },
])
const prevHost = useRequestHeaders()["x-forwarded-proto"];
const host = useRequestHeaders()["host"];
const fullPath = useRoute()["fullPath"];
const title = ref('题库中心');
const keywords = ref('题库中心');
const description = ref('题库中心');
const all = useRoute().params.all;
const examNotice = ref(`<p><strong style="color: #ff4e00;">1. 遵守考试规定：</strong>在考试期间，考生需要遵守考试规定，如不得随意切屏、返回上一级等与考试答题无关的操作，如违规者统统视为考生自愿提前交卷处理。</p>
    <p><strong style="color: #ff4e00;">2. 注意考试时间：</strong>考试时间通常比较紧张，考生需要合理规划时间，确保能在规定时间内完成考试。</p>
    <p><strong style="color: #ff4e00;">3. 注意题目要求：</strong>在答题过程中，考生需要仔细阅读题目要求，理解题意后再进行答题。</p>
    <p><strong style="color: #ff4e00;">4. 做完后检查：</strong>在完成答题后，考生需要留出一定时间进行答案检查，及时发现并纠正错误。</p>
    <p><strong style="color: #ff4e00;">5. 注意个人信息保护：</strong>在考试过程中，考生需要保护好个人信息，如密码、账号等，以免信息泄露导致考试成绩异常。</p>`)
definePageMeta({
  validate: async (route) => {
    let flag = true;
    route.params.all.forEach(item => {
      if (!/^1\d{5,}$/.test(item)) {
        flag = false;
        return;
      }
    })
    return flag;
  }
})
const examHref = ref(useToken().token ? `/exam/${all[2] || all[1] || all[0]}.html` : '/login');
const subjectData = ref([])
const subjectTwoData = ref([])
const subjectThreeData = ref([])
const exercisesCountData = ref({})
const hrefFlag = ref(false)
const allHref = ref(null)
const selectedHref = ref(null)
const doneHref = ref(null)
const correctHref = ref(null)
const examModalVisible = ref(false)
// 获取科目
async function getSubject() {
  let { data: subjectRes } = await useAsyncData('getQuestionListApi', () => useServerRequest(getQuestionListApi, {
    method: 'post',
    body: JSON.stringify({
      pageNum: 1,
      pageSize: 666,
      exerciseOpen: true
    })
  }))
  if (subjectRes.value && subjectRes.value.errno === 0 && subjectRes.value.result && subjectRes.value.result.total > 0) {
    subjectData.value = treeFn(subjectRes.value.result.list, 0, []);
    // 获取二级
    subjectData.value.forEach(item => {
      if (item.id == all[0]) {
        // children不存在 token存在
        if (!item.children && useToken().token) {
          hrefFlag.value = true;
        }
        // TDK
        title.value += `_${item.name}`;
        keywords.value += `,${item.name}`;
        description.value += `，${item.name}`;
        if (item.children) {
          item.children.forEach(ite => {
            ite.href = `/exercises/${all[0]}/${ite.id}`;
          })
          subjectTwoData.value = item.children;
        }
      }
    })
    // 获取三级
    subjectTwoData.value.forEach(item => {
      if (item.id == all[1]) {
        // children不存在 token存在
        if (!item.children && useToken().token) {
          hrefFlag.value = true;
        }
        // TDK
        title.value += `_${item.name}`;
        keywords.value += `,${item.name}`;
        description.value += `，${item.name}`;
        if (item.children) {
          item.children.forEach(ite => {
            ite.href = `/exercises/${all[0]}/${all[1]}/${ite.id}`;
            if (ite.id == all[2]) {
              // children不存在 token存在
              if (!ite.children && useToken().token) {
                hrefFlag.value = true;
              }
              // TDK
              title.value += `_${ite.name}`;
              keywords.value += `,${ite.name}`;
              description.value += `，${ite.name}。`;
            }
          })
          subjectThreeData.value = item.children;
        }
      }
    })
    return false;
  }
}
getSubject()
// 获取习题数量
async function getExercisesCount() {
  let { data: exercisesCountRes } = await useAsyncData('getCountApi', () => useServerRequest(getCountApi, {
    method: 'get',
    params: {
      subjectId: all[2] || all[1] || all[0]
    }
  }))
  if (exercisesCountRes.value && exercisesCountRes.value.errno === 0 && exercisesCountRes.value.result) {
    exercisesCountData.value = exercisesCountRes.value.result;
    // 末尾类别
    allHref.value = hrefFlag.value && exercisesCountRes.value.result.all > 0 ? `/exercises_details/all-${all[2] || all[1] || all[0]}.html` : null;
    selectedHref.value = hrefFlag.value && exercisesCountRes.value.result.selected > 0 ? `/exercises_details/selected-${all[2] || all[1] || all[0]}.html` : null;
    // doneHref.value = hrefFlag.value && exercisesCountRes.value.result.notDone > 0 ? `/exercises_details/notDone-${all[2] || all[1] || all[0]}.html` : null;
    correctHref.value = hrefFlag.value && exercisesCountRes.value.result.wrong > 0 ? `/exercises_details/wrong-${all[2] || all[1] || all[0]}.html` : null;
    return false;
  }
}
getExercisesCount()
// 选择习题提示
function exercisesFn(type) {
  if (!useToken().token) {
    navigateTo('/login')
    ElMessage.error('请先登录')
    return false
  }
  if (!hrefFlag.value) {
    ElMessage.warning('请继续选择习题类别')
    return false
  }
  if (type == 'all' && !allHref) { // 全部
    ElMessage.warning('未发现全部习题')
    return false
  }
  if (type == 'selected' && !selectedHref.value) { // 精选
    ElMessage.warning('未发现精选练题')
    return false
  }
  if (type == 'done' && !doneHref.value) { // 未做
    ElMessage.warning('未发现未做习题')
    return false
  }
  if (type == 'correct' && !correctHref.value) { // 错题
    ElMessage.warning('未发现我的错题')
    return false
  }
}
// 清除习题记录
function clearExamFn() {
  ElMessageBox.confirm('是否清除习题记录？', '系统提示').then(async () => {
    let res = await useServerRequest(clearExerciseRecord, {
      method: 'get',
      params: {
        subjectId: all[2] || all[1] || all[0]
      }
    })
    if (res.errno === 0) {
      ElMessage.success('清除成功')
      return false
    }
  }).catch(() => { })
}
// 模拟考试弹窗
function examModalFn() {
  examModalVisible.value = true;
}
// 开始考试
function examStartFn() {
  window.location.href = examHref.value;
  examModalVisible.value = false;
  localStorage.setItem('examId', all[2] || all[1] || all[0])
}
// TDK
useSeoMeta({
  title,
  keywords,
  description,
  ogTitle: title,
  ogDescription: description,
  ogType: "article",
  ogUrl: `${prevHost}://${host}${fullPath}`,
});
</script>

<style lang="less" scoped>
p {
  margin: 0;
}

.con {
  min-height: calc(100vh - 454px);
  padding: 20px 0;

  &_cen {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    align-items: flex-start;

    &_left {
      width: 300px;
      min-height: 460px;
      margin-right: 20px;
      background: #FFFFFF url('https://static.dzyxzx.cn/jkbpc/menu_b.png') 0 0 / 100% no-repeat;
      box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.03);
      border-radius: 10px;

      &_top {
        padding: 44px 0 0 25px;
        height: 156px;
        display: flex;

        &_icon {
          width: 59px;
          height: 58px;
          margin-right: 11px;
        }

        &_item {
          flex: 1;
          overflow: hidden;

          >strong {
            font-size: 26px;
            height: 26px;
            line-height: 26px;
          }

          >p {
            margin-top: 12px;
            font-size: 16px;
          }
        }
      }

      &_bottom {
        padding: 40px 25px 0;

        &_ul {

          &_li {
            margin-bottom: 12px;
            border-bottom: 10px solid #F5F5F5;
            overflow: hidden;

            &:last-child {
              padding-bottom: 0;
              border-bottom: 0;
            }

            .el-button {
              white-space: normal;
              height: auto;
              text-align: left;
              line-height: 14px;
            }

            .el-button {
              margin: 0 12px 12px 0;

              &+.el-button {
                margin: 0 12px 12px 0;
              }
            }
          }
        }
      }
    }

    &_right {
      flex: 1 0;
      background: #FFFFFF;
      box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.03);
      border-radius: 10px;
      padding: 50px 24px;
      min-height: 460px;

      &_top {
        display: flex;
        align-items: stretch;
        justify-content: space-between;

        &_link {
          flex: 410px 0;
          height: 100px;
          border-radius: 10px;
          padding-left: 95px;
          padding-top: 36px;
          display: flex;
          align-items: baseline;
          overflow: hidden;
          cursor: pointer;

          &:nth-child(1) {
            background: url('https://static.dzyxzx.cn/jkbpc/exam_a.png') 0 0 / 410px no-repeat;
          }

          &:nth-child(2) {
            background: url('https://static.dzyxzx.cn/jkbpc/exam_b.png') 0 0 / 410px no-repeat;
          }

          >strong {
            color: #FFFFFF;
            font-weight: bold;
            font-size: 26px;
          }

          >span {
            margin-left: 10px;
            color: #FFFFFF;
            font-size: 20px;
          }
        }
      }

      &_bottom {
        margin-top: 30px;
        display: flex;
        align-items: stretch;

        &_link {
          flex: 270px 0;
          height: 100px;
          background-color: #F5F5F5;
          border-radius: 10px;
          overflow: hidden;
          display: flex;
          align-items: center;
          padding: 20px;
          margin-right: 12px;
          cursor: pointer;

          &:last-child {
            margin-right: 0;
          }

          &_icon {
            width: 60px;
            height: 60px;
            margin-right: 14px;
          }

          &_item {
            flex: 1 0;
            overflow: hidden;

            >strong {
              font-weight: bold;
              font-size: 24px;
              height: 24px;
              line-height: 24px;
              color: #666666;
            }

            >p {
              font-size: 14px;
              height: 14px;
              line-height: 14px;
              color: #666666;
              margin-top: 9px;
            }
          }
        }
      }

      &_clear {
        text-align: center;
        margin-top: 20px;

        &_link {
          color: #999;
          font-size: 14px;
          cursor: pointer;
        }
      }
    }
  }
}
</style>